package de.gwtprime.server.servlets.taskqueue;

import java.io.IOException;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.gwtprime.server.util.MailSender;

/**
 * This worker is used in the task queue to queue and asynchronously 
 * send email.
 * 
 * Can be used like this:
 * 
 * QueueFactory.getDefaultQueue().queue.add(
 * 		TaskOptions.Builder.withUrl("/mailWorker").
 *				param("recipient", "xyz@test.de").
 *				param("subject", "yourSubject").
 *				param("text", "emailText");
 *		
 */
public class EmailWorker extends HttpServlet{

	/**
	 * Serial version ID
	 */
	private static final long serialVersionUID = 1L;
	
	private static Logger logger = Logger.getLogger(EmailWorker.class
			.getName());

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		// Getting the parameters
		String text = req.getParameter("text");
		String recipient = req.getParameter("recipient");
		String subject = req.getParameter("subject");
		
		logger.info(String.format("Sending email with options: recipient: '%s', subject: '%s', text: '%s'", 
				recipient, subject, text));
		
		MailSender.sendMail(
				recipient, 
				subject, 
				text);

		logger.info("Sending complete");
	}
}
